//js for upload files

var ocUpload = {
	init : ocUploadInit,
	add : ocAddFile,
	remove : ocRemoveFile,
	check : ocFileCheck
}

function ocUploadInit(maxUpfiles){
	//maxUpfiles = 5;	//set max upload files

	var html = "<table id=\"oc_uploadTable\" class=\"tebel\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"
		+ "    <INPUT TYPE=\"button\" id=\"oc_addbtn\" NAME=\"oc_addbtn\" class=\"button1\" value=\"添加一条\" \n"
		+ "	   onclick=\"javascript:ocUpload.add('oc_uploadTable','oc_fileCounter'," + maxUpfiles + ");\">\n"
		+ "	   <a href=\"select_news.php\" target=\"_blank\" >搜索新闻</a>\n"
		+ "    <tr><td> &nbsp;新闻标题：<INPUT TYPE=\"text\" id=\"news_title1\" NAME=\"news_title1\" size=\"40\" class=\"button1\">\n"
		+ "    链接地址：<INPUT TYPE=\"text\" id=\"news_url1\" NAME=\"news_url1\" size=\"40\ class=\"input\">\n"
		+ "    <INPUT TYPE=\"hidden\" id=\"news_num1\" NAME=\"news_num1\">\n"
		+ "	   <a href=\"#\" onClick=\"javascript:ocUpload.remove('oc_uploadTable',this.parentNode.parentNode.parentNode);\">移除</a>\n"
		+ "    </td></tr>\n"
		+ "</table>\n"
		
		+ "<input type=\"hidden\" name=\"oc_fileCounter\" id=\"oc_fileCounter\" value=\"1\">\n";
	document.write(html);
}

		

function ocAddFile(tableId, counterId, maxNum){
	var counter = $(counterId).value;

	var tableObj = $(tableId);
	var td = document.createElement("td");
	var tr = document.createElement("tr");
	var tbody = document.createElement("tbody");
	counter ++;
	document.getElementById('oc_fileCounter').value=counter;
	var html = " &nbsp;新闻标题：<INPUT TYPE=\"text\" size=\"40\" id=\"news_title"
		 + counter + "\" NAME=\"news_title"
		 + counter + "\" class=\"button1\"> \n"
		 + "链接地址：<INPUT TYPE=\"text\" size=\"40\" id=\"news_url"
		 + counter + "\" NAME=\"news_url"
		 + counter + "\" class=\"button1\">"
		 + "<INPUT TYPE=\"hidden\" id=\"news_num"
		 + counter + "\" NAME=\"news_num"
		 + counter + "\">"
		// + "<INPUT id=\"oc_fileCounter\" NAME=\"oc_fileCounter\" value="+counter+">"		 
		 + "&nbsp;<a href='#' onClick=\""
		 + "javascript:ocUpload.remove('" + tableId 
		 + "',this.parentNode.parentNode.parentNode);\">移除</a>";
	td.innerHTML = html;

	tr.appendChild(td);
	tbody.appendChild(tr);

	var tchild = tableObj.getElementsByTagName("tbody");
	var childNum = tchild.length;

	if(ocFileCheck(counterId)){
		if(childNum < maxNum){
			//tableObj.insertBefore(tbody,tableObj.firstChild);
			tableObj.appendChild(tbody);	//add file
			setValue(counterId, counter);	//set counter++
		}else{
			alert("已达到上限数："+maxNum+"个!");
			return false;
		}
	}
}

function ocRemoveFile(tableId, node){
	var tableObj = $(tableId);
	tableObj.removeChild(node);
}

function ocFileCheck(counterId){
	var counter = $(counterId).value;

	var i;
	var fileOk = 0;
	var nullId;
	for(i=1; i<=counter; i++){
		try{
			if($("news_url"+i).value == ""){
				fileOk = 1;
				nullId = i;
			}
		}catch(Exception){}
	}

	if(fileOk == 1){
		alert("已有未用链接！");
		$("botton"+nullId).focus();
		return false;
	}

	return true;
}